﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using FX.CC.Andon.Kanban.KanbanSvcRef;
using System.Threading;
using System.Media;
using System.Reflection;

namespace FX.CC.Andon.Kanban
{
    public partial class frmTaskInfo : DevExpress.XtraEditors.XtraForm
    {
        int clickCount = 0;
        int restartClickCount = 0;

        System.Timers.Timer tmr;
        Action actUpdateTime;

        #region About Paint
        string NoticeStr = string.Empty;
        Font noticeFont = new Font("微软雅黑", 20, FontStyle.Bold);
        SolidBrush welBrush = new SolidBrush(Color.Red);
        int currentX = 1;
        SizeF contentSize;
        bool contentWidthGot;
        #endregion

        public frmTaskInfo()
        {
            InitializeComponent();
            this.gridControl2.Dock = DockStyle.Fill;
            this.Location = new Point(0, 0);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            lbTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分");
            lbVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            
        }

        public void SetDataSource(IList<TaskKanbanWrapper> set, bool playNotice, string noticeStr)
        {
            gridControl2.DataSource = set;
            gridView2.BestFitColumns();

            if (playNotice)
            {
                if (noticeStr != this.NoticeStr)
                {
                    currentX = 1;
                    this.NoticeStr = noticeStr;
                    contentWidthGot = false;
                    pnlNotice.Visible = true;
                    timer2.Start();
                }
            }
            else {
                pnlNotice.Visible = false;
                timer2.Stop();
                this.NoticeStr = string.Empty;
            }
        }

        private void lbVersion_DoubleClick(object sender, EventArgs e)
        {
            if (++clickCount >= 5) {
                clickCount = 0;
                //this.Close();
                Application.Exit();
            }
        }

        private void gridView2_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            var wrapper = gridView2.GetRow(e.RowHandle) as KanbanWrapper;
            if(wrapper == null)return;
            if (string.IsNullOrEmpty(wrapper.SignUserName))
            {
                e.Appearance.BackColor = Color.Crimson;
            }
        }

        private void gridView2_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right) {
                if (Program.cursorShown)
                {
                    Cursor.Hide();
                }
                else {
                    Cursor.Show();
                }
                Program.cursorShown = !Program.cursorShown;
            }
        }

        private void picLogo_DoubleClick(object sender, EventArgs e)
        {
            if (++restartClickCount >= 5)
            {
                restartClickCount = 0;
                if (_MsgBox.PopQuestion("是否重启看板控制器？") != DialogResult.Yes)
                {
                    return;
                }
                
                System.Diagnostics.Process.Start("Shutdown.exe", @" -r -t 0");
            }
        }

        private void lbTime_DoubleClick(object sender, EventArgs e)
        {
            using (var frm = new frmConfig()) {
                frm.ShowDialog();
            }
        }

        private void frmTaskInfo_Shown(object sender, EventArgs e)
        {
            tmr = new System.Timers.Timer(60*1000);
            tmr.Elapsed += new System.Timers.ElapsedEventHandler(tmr_Elapsed);
            tmr.Start();
            actUpdateTime = new Action(() => {
                lbTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分"); 
            });
        }

        void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try {
                this.Invoke(actUpdateTime);
            }
            catch { 
                
            }
        }

        private void frmTaskInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                tmr.Stop();
                tmr.Dispose();
            }
            catch { }
        }

        private void panelControl2_Paint(object sender, PaintEventArgs e)
        {
            int parentWidth = pnlNotice.Width;
            if (!contentWidthGot)
            {
                contentSize = e.Graphics.MeasureString(NoticeStr, noticeFont);
                contentWidthGot = true;
            }
            using (Bitmap bmp = new Bitmap(parentWidth, pnlNotice.Height))
            {
                using (var gp = Graphics.FromImage(bmp))
                {
                    gp.Clear(pnlNotice.BackColor);
                    currentX -= 5;
                    gp.DrawString(NoticeStr, noticeFont, welBrush, currentX, 5);
                    bool isLongStr = contentSize.Width > (float)parentWidth;
                    if (currentX < 0)
                    {
                        gp.DrawString(NoticeStr, noticeFont, welBrush, (isLongStr ? contentSize.Width : parentWidth) + currentX, 5);
                    }
                    if (currentX < -contentSize.Width)
                    {
                        currentX += (isLongStr ? (int)contentSize.Width : parentWidth);
                    }
                    e.Graphics.DrawImage(bmp, 0, 0);
                }
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (pnlNotice.Visible)
            {
                pnlNotice.Refresh();
            }
        }
        
    }
}
